package dao.impl;

import bean.pojo.Room;
import commons.dbHelper.DBHelper;
import dao.RoomDao;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.List;


/**
 * dao.impl
 * User: 坚持
 * Date: 2024/12/17 11:44
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
public class RoomDaoImpl implements RoomDao {

    /**
     * 修改房产信息
     */
    @Override
    public Integer update(Room room) {
        String sql ="update room set room_number=?,room_area=?,room_type=?," +
                "user_id=?,remarks=?, where room_id=?";
        DBHelper dbHelper = new DBHelper();
       return dbHelper.update(sql,room.getRoomNumber(),room.getRoomArea(),
               room.getRoomType(),room.getUserId(),room.getRemarks(),
               room.getRoomId());
    }

    @Override
    public Integer add(Room room) {
        String sql ="insert room  (room_number,room_area,room_type," +
                "user_id,remarks) values (?,?,?,?,?)";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,room.getRoomNumber(),room.getRoomArea(),
                room.getRoomType(),room.getUserId(),room.getRemarks());
    }

    @Override
    public List<Room> roomFindBy(Integer page, Integer limit, Integer roomId, String roomNumber, String roomType, Integer roomArea, String remarks, Integer userId) {
        StringBuffer sql = new StringBuffer("select room_id,room_number,room_area,room_type,user_id,remarks from room where 1=1");
        List<Object>  parameters =new ArrayList<>();
        if (roomId != null) {
            sql.append(" and room_id=? ");
            parameters.add(roomId);
        }
        if (userId != null) {
            sql.append(" and user_id=? ");
            parameters.add(userId);
        }
        if (roomNumber != null) {
            sql.append(" and room_number like concat ('%',?,'%') ");
            parameters.add(roomNumber);
        }
        if (roomType != null) {
            sql.append(" and room_type like concat ('%',?,'%') ");
            parameters.add(roomType);
        }
        if (roomArea != null) {
            sql.append(" and room_area=? ");
            parameters.add(roomArea);
        }
        if (remarks != null) {
            sql.append(" and remarks like concat ('%',?,'%') ");
            parameters.add(remarks);
        }
        sql.append(" order by room_id asc ");
        sql.append(" limit " + (page - 1) * limit + "," + limit);
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        return dbHelper.getBeanList(Room.class,sql.toString(),parameters.toArray());//想知道不写to会发送什么
    }

    @Override
    public Long findByTotal(Integer roomId, String roomNumber, String roomType, Integer roomArea, String remarks, Integer userId) {
        StringBuffer sql = new StringBuffer("select room_id,room_number,room_area,room_type,user_id,remarks from room where 1=1");
        List<Object>  parameters =new ArrayList<>();
        if (roomId != null) {
            sql.append(" and room_id=? ");
            parameters.add(roomId) ;
        }
        if (userId != null) {
            sql.append(" and user_id=? ");
            parameters.add(userId);
        }
        if (roomNumber != null) {
            sql.append(" and room_number like concat ('%',?,'%') ");
            parameters.add(roomNumber);
        }
        if (roomType != null) {
            sql.append(" and room_type like concat ('%',?,'%') ");
            parameters.add(roomType);
        }
        if (roomArea != null) {
            sql.append(" and room_area>=? ");
            parameters.add(roomArea);
        }
        if (remarks != null) {
            sql.append(" and remarks like concat ('%',?,'%') ");
            parameters.add(remarks);
        }
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        List<Room> query = dbHelper.getBeanList(Room.class,sql.toString(),parameters.toArray());
        if (query!=null) {
            return (long) query.size();
        }
        return 0L;
    }

    @Override
    public Integer deleteById(Integer roomId) {
        String sql = " delete from room where room_id = ? ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,roomId);
    }

    @Override
    public List<Room> findType() {
        String sql = " select distinct room_type from room ";
        DBHelper dbHelper = new DBHelper();
        List<Room> list = dbHelper.getBeanList(Room.class,sql);
        System.out.println("List<String>:"+list);
        return list;
    }

    @Override
    public List<Room> findRoomList() {
        String sql = " select room_id,room_number from room order by room_id asc";
        DBHelper dbHelper = new DBHelper();
        List<Room> list = dbHelper.getBeanList(Room.class,sql);
        return list;
    }


}
